package com.semeureka.fault.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.semeureka.fault.bean.Group;
import com.semeureka.fault.dao.GroupDao;

@Component
public class GroupDaoImpl implements GroupDao {
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public Group findByHostCode(byte[] hostCode) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Group.class);
		criteria.add(Restrictions.eq("hostCode", hostCode));
		criteria.addOrder(Order.asc("number")).setMaxResults(1);
		return (Group) criteria.uniqueResult();
	}

	@Override
	public Group findByGroupNumber(Group group, Integer number) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Group.class);
		criteria.add(Restrictions.eq("hostCode", group.getHostCode()));
		criteria.addOrder(Order.asc("number")).setFirstResult(number - 1).setMaxResults(1);
		return (Group) criteria.uniqueResult();
	}
}
